<?php
namespace Shopmanage\Controller;
use Think\Controller;
class EventsetController extends ExtendController {
	public function setcur(){
		$setting = I("setting");
		$CFG = D("Shopmanage/ShopCfg");
		$CFG->curtool=$setting;
		$CFG->sid = $this->sid;
		$CFG->taobao_user_id=$this->taobao_user_id;
		$CFG->usernick=$this->usernick;
		$CFG->sessionkey=session("access_token");
		$CFG->refresh_token=session("refresh_token");
		$data = $CFG->setcur();
		$this->ajaxReturn($data);
	}
	public function check_time(){
		$eid = (int)I("eid");
		$start_time = I("start_time");
		$end_time = I("end_time");
		$time = $start_time?$start_time:$end_time;
		if($start_time && $end_time){
			if(strtotime($start_time)>strtotime($end_time)){
				$err = "开始时间不能晚于结束时间！";
			}elseif(strtotime($start_time)==strtotime($end_time)){
				$err = "开始时间不能和结束时间相同！";
			}
			if($err){
				$data["hasError"]=true;
				$data["msg"]=$err;
				$this->ajaxReturn($data);
				die();
			}
			$where = "`sid`=".$this->sid." AND (`start_time` BETWEEN '".$start_time."' AND '".$end_time."' OR `end_time` BETWEEN '".$start_time."' AND '".$end_time."')";
		}else{
			$where = "`sid`=".$this->sid." AND ('".$time."' BETWEEN `start_time` AND `end_time`)";
		}
		$where .= $eid?" AND `id`<>$eid":""; //修改活动时排除当前活动
		
		$EVENT = M("Events");
		$time_duble = current($EVENT->where($where)->order("`id`")->getField("`id`,`start_time`,`end_time`"));
		if($time_duble){
			$data["hasError"]=true;
			$data["where"]=I("tp");
			if($data["where"]=="start-calendar"){
				$data["msg"]="时间段内已有活动，请重新设置时间，或<a href=\"###\" class=\"set_time\" data-tp=\"".I("tp")."\">填入最近可开始的时间</a>。";
				$data["time"]=date("Y-m-d H:i:s",strtotime($time_duble["end_time"])+1);
			}else{
				$data["msg"]="在".date("Y-m-d H:i:s",strtotime($time_duble["start_time"]))."已有活动，请重新设置时间，或<a href=\"###\" class=\"set_time\" data-tp=\"".I("tp")."\">填入此时间</a>。";
				$data["time"]=date("Y-m-d H:i:s",strtotime($time_duble["start_time"])-1);
			}
		}else{
			$data["success"]=true;
		}
		// $data["sql"]=$EVENT->_sql();
		$this->ajaxReturn($data);
	}
	public function set_event_gift(){
		$action = I("action");
		$egid = I("egid");
		if(!session("lott_event_id")){
			$reda["hasError"]=true;
			$reda["error"]="没有活动ID！请重新创建活动";
			$this->ajaxReturn($reda);
			die();
		}
		$data["name"] = I("gift_name");
		$data["gift_id"] = I("gift_id");
		$data["level"] = I("level_num"); //0：不显示等级    implicat：隐含奖品，不显示在奖品列表
		$data["level_name"] = I("level_name"); //隐含奖品等级标题为：“隐含奖品”
		if($data["level"]=="implicat"){
			$data["level"]=0;
			$data["status"] = 1; //隐含
		}
		$data["type"] = I("gift_type");
		$data["pic_url"] = I("pic_url");
		$data["quantity"] = I("quantity");
		$data["quantity_initial"] = I("quantity");
		$data["probability"] = I("probability")*100000;
		$data["per_user"] = I("per_user"); //每人最多中出
		$data["advance"] = I("advance");
		switch($data["advance"]){
			case "time-range":
				$data["range_time_start"] = I("range_time_start");
				$data["range_time_end"] = I("range_time_end");
				$data["advance_quantity"] = I("advance_quantity");
				$data["advance_probability"] = I("range_probability")*100000;
			break;
			case "ev-minute" :
				$data["every_minute"] = I("every_minute");
				$data["advance_quantity"] = I("advance_quantity");
			break;
			case "ev-lotts":
				$data["every_lotts"] = I("every_lotts");
				$data["advance_quantity"] = I("advance_quantity");
			break;
		}
		$data["ext_rule"] = I("ext_rule")?I("ext_rule"):null;
		switch($data["ext_rule"]){
			case "order":
				$data["payment"] = I("payment");
				$data["pay_time_start"] = I("pay_time_start")?I("pay_time_start"):NULL;
				$data["pay_time_end"] = I("pay_time_end")?I("pay_time_end"):null;
				$data["order_items"] = I("order_items")?I("order_items"):null;
			break;
			case "jifen":
				$data["jifen"] = I("jifen");
			break;
			case "password":
				$data["password"] = I("passwd");
			break;
			case "metal":
				$data["medal_id"] = I("medal_id");
				$data["medal_name"] = I("medal_name");
				$data["medal_pic"] = I("medal_pic");
				$data["medal_quant"] = I("medal_quant");
			break;
			case "collect":
				$data["collect_items"] = I("collect_items");
			break;
			case "addcart":
				$data["cart_items"] = I("cart_items");
			break;
			case "friend_show":
				$data["show_title"] = I("show_title");
				$data["show_content"] = I("show_content");
			break;
		}
		$data["rule_medal_id"] = I("rule_medalid");
		$data["rule_medal_name"] = I("rule_medal_name");
		$data["rule_medal_pic"] = I("rule_medal_pic");
		
		$data["num_iids"] = "";
		$data["event_id"] = session("lott_event_id");
		$data["sid"] = $this->sid;
		$EGIFT = M("EventsGifts");
		if($action=="add")
		{
			$re = $EGIFT->add($data);
			if($re){
				$GIFT = M("Gifts");
				$GIFT->where("`id`=".$data["gift_id"]." AND `sid`=".$this->sid)->setDec("quantity",$data["quantity"]);
				$reda["hasError"]=false;
			}else{
				$reda["hasError"]=true;
				$reda["error"]="添加奖品出错！".$EGIFT->getDbError();;
			}
		}
		elseif($action=="edit")
		{
			if($egid>0){
				$last_quant = I("last_quant");
				$data["id"]=$egid;
				$re = $EGIFT->save($data);
				if($re)
				{
					$sys_quant = $last_quant-$data["quantity"];
					$GIFT = M("Gifts");
					$where = Array("id"=>$data["gift_id"],"sid"=>$this->sid);
					if($sys_quant>0){
						$GIFT->where($where)->setInc("quantity",abs($sys_quant));
					}else{
						$GIFT->where($where)->setDec("quantity",abs($sys_quant));
					}
					$reda["hasError"]=false;
				}else{
					$reda["hasError"]=true;
					$reda["error"]="修改奖品出错！".$EGIFT->getDbError();;
				}
			}else{
				$reda["hasError"]=true;
				$reda["error"]="没有奖品ID";
			}
		}
		$this->ajaxReturn($reda);
	}
	public function get_event_gift(){
		$id = I("id");
		$EGIFT = M("EventsGifts");
		$re = $EGIFT->where(Array("id"=>$id,"sid"=>$this->sid))->find();
		$re["advance_quantity"] = $re["advance_quantity"]?$re["advance_quantity"]:0;
		$re["probability_show"] = $re["probability"]/100000;
		if(C("TAE")){
			$re["order_items_count"] = count(json_decode(urldecode_utf8($re["order_items"]),true));
			$re["collect_items_count"] = count(json_decode(urldecode_utf8($re["collect_items"]),true));
			$re["cart_items_count"] = count(json_decode(urldecode_utf8($re["cart_items"]),true));
		}else{
			$re["order_items_count"] = count(json_decode(urldecode($re["order_items"]),true));
			$re["collect_items_count"] = count(json_decode(urldecode($re["collect_items"]),true));
			$re["cart_items_count"] = count(json_decode(urldecode($re["cart_items"]),true));
		}
		$this->ajaxReturn($re);
	}
	public function del_event_gift(){
		$id = I("id");
		$fid = I("fid");
		$quant = I("quant");
		$UGFT = M("UsersGifts");
		$hasWined = $UGFT->where(Array("sid"=>$this->sid,"giftid"=>$id))->limit(1)->getField("id");
		if($hasWined){
			$data["hasError"]=true;
			$data["error"]="已经有人中出这个奖品，不允许删除！";
			$this->ajaxReturn($data);
			die();
		}
		$EGIFT = M("EventsGifts");
		$re = $EGIFT->where("`id`=".$id." AND `sid`=".$this->sid)->delete();
		if($re){
			$GIFT = M("Gifts");
			$GIFT->where("`id`=".$fid." AND `sid`=".$this->sid)->setInc("quantity",$quant);
			$data["hasError"]=false;
		}else{
			$data["hasError"]=true;
			$data["error"]="删除奖品出错！";
		}
		$this->ajaxReturn($data);
	}
	public function gamewg_save_kg(){
		$picurl = I("picurl");
		$eid = I("eid");
		$data["front_pic"]=I("status");
		$data["front_pic_url"]=I("picurl");
		$CFG = M("Events");
		$data["status"] = $CFG->where(Array("sid"=>$this->sid,"id"=>$eid))->save($data);
		$this->ajaxReturn($data);
	}	
	public function anmicfg_save(){
		$status = I("status");
		$eid = I("eid");
		$data["none_anim"]=(int)$status;
		$CFG = M("Events");
		$re["status"] = $CFG->where(Array("sid"=>$this->sid,"id"=>$eid))->save($data);
		$re["anmicfg"]=$status;
		$this->ajaxReturn($re);
	}
}